package com.wowudi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.dto.Result;
import com.wowudi.entity.Role;
import com.wowudi.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "角色管理")
@RequestMapping("api/system/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "查询所有角色")
    @GetMapping("getRoles")
    public Result getRoles(){
        Result result = roleService.findAll();
        return result;
    }


    @ApiOperation(value = "查询所有角色带分页带条件")
    @GetMapping("getRolePageByName")
    public Result getRolePageByName(int currentPage, int limit, String name){
        Page<Role> rolePage = roleService.findPageByName(currentPage, limit, name);
        Result result = new Result();
        result.setData(rolePage);
        result.setMessage("成功");
        return result;
    }


    @ApiOperation(value = "添加角色信息")
    @PostMapping("addRole")
    public Result addRole(@RequestBody Role role,Integer[] menuIds){
        int i = roleService.addRole(role, menuIds);
        Result result = new Result();
        if (i == 1) {
            result.setMessage("添加角色成功！");
        }else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }

    @ApiOperation(value = "修改角色信息")
    @PostMapping("updateRole")
    public Result updateRole(@RequestBody Role role,Integer[] menuIds){
        int i = roleService.updateRole(role, menuIds);
        Result result = new Result();
        if (i == 1) {
            result.setMessage("修改角色成功！");
        }else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }

    @ApiOperation(value = "删除角色信息")
    @PostMapping("deleteRole")
    public Result deleteRole(Integer id){
        int i = roleService.deleteRole(id);
        Result result = new Result();
        if (i == 1) {
            result.setMessage("删除角色成功！");
        }else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }
}
